import java.util.ArrayList;
import java.util.Scanner;

public class ATMTest{
    public static void main(String[] args){
        ATM atm = new ATM();
        atm.execute();
    }
}

//User类
class User{
    String userName;        //用户名
    String password;        //密码
    String cardId;          //卡号
    long balance;           //余额
    
    //User构造方法
    User(String userName, String password, String cardId){
        this.userName = userName;
        this.password = password;
        this.cardId = cardId;
        this.balance = 0;    
    }
    
    //存款方法，amount参数为存款额度
    void deposit(long amount) throws Exception{
        if(amount >= 0){
            this.balance += amount;
        }else{
            //如果存款额度出现问题，抛出异常和异常信息
            throw new Exception("取款失败：取款额度错误");
        }
    }
    
    //取款方法，amount为取款额度
    void withdraw(long amount) throws Exception{
        if(amount >= 0 && amount <= this.balance){
            balance -= amount;
        }else{
            //如果取款额度出现问题，抛出异常与异常信息
            throw new Exception("取款失败：取款额度错误或余额不足");
        }
    }
    
    //返回余额
    long theBlance(){
        return this.balance;
    }
    
}

class ATM{
    ArrayList<User> userList;           //存储User用户
    User currentUser;                   //当前用户引用
    Scanner scan;                       //Scanner实例
    
    //ATM构造方法
    public ATM(){
        this.userList = new ArrayList<User>();
        this.currentUser = null;
        scan = new Scanner(System.in);
    }
    
    //ATM启动方法
    public void execute(){
        
        boolean out = false;
        int choose;
        while(!out){
            /*
                判断是否存在当前用户，如果不存在，则需要创建或登陆
                                    如果存在，则打印用户操作菜单
            */
            if(currentUser == null){        
                do{
                    System.out.println(); 
                    System.out.println("1.创建用户\n"+
                                       "2.登陆\n"+
                                       "3.退出");
                    choose = scan.nextInt();
                }while(choose > 3 || choose < 1);
                
                switch(choose){
                    //调用 创建用户 视图
                    case 1: createUserView(); break;
                    //调用 登陆 视图
                    case 2: loginView(); break;
                    //退出则停止循环
                    case 3: out = true; break;
                    default: break;
                }
            }else{
                do{
                    System.out.println();                    
                    System.out.println("1.存款\n"+
                                      "2.取款\n"+
                                      "3.查看余额\n"+
                                      "4.退出");
                    choose = scan.nextInt();
                }while(choose > 4 || choose < 1);
                
                switch(choose){
                    //调用 存款 视图
                    case 1: depositView(); break;
                    //调用 取款 视图
                    case 2: withdrawView(); break;
                    //调用 查看余额 视图
                    case 3: theBlanceView(); break;
                    //退出，当前用户注销
                    case 4: currentUser = null; break;
                    default: break;
                }
            }
        }
        
        currentUser = null;
    }
    
    //创建用户 视图
    protected void createUserView(){
        System.out.println();        
        System.out.println("创建用户");
        
        System.out.print("用户名: ");
        String userName = scan.next();
        
        System.out.print("密码: ");
        String password = scan.next();
        
        try{
            String cardId = createUserController(userName, password);
            System.out.println("请记住您的卡号: " + cardId);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    //创建用户 控制器
    protected String createUserController(String userName, String password) throws Exception{
        
        String userNameRegExp = "[0-9a-zA-Z\\_]+";
        if(!userName.matches(userNameRegExp)){
            throw new Exception("用户名错误：用户名只允许出现字母，数字，下划线，且长度至少为1");
        }
        
        String passwordRegExp = "[0-9a-zA-Z\\_]{6,20}";
        if(!password.matches(passwordRegExp)){
            throw new Exception("密码错误：密码只允许出现字母，数字，下划线，且密码长度为6-20");
        }
        
        String cardId = String.valueOf(userList.size());
        
        User tmp = new User(userName, password, cardId);
        userList.add(tmp);
        
        return cardId;
    }
    
    //登陆 视图
    protected void loginView(){
        System.out.println();        
        System.out.println("登陆");
        
        System.out.print("用户名: ");
        String userName = scan.next();
        
        System.out.print("密码: ");
        String password = scan.next();
        
        System.out.print("卡号: ");
        String cardId = scan.next();
        
        try{
            loginController(userName, password, cardId);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    //登陆 控制器
    protected void loginController(String userName, String password, String cardId) throws Exception{
        
        int index = cardIdIndexOf(cardId);
        if(index < 0){
            throw new Exception("账户错误：不存在此卡号");
        }
        
        User tmp = userList.get(index);
        if(!tmp.userName.equals(userName)){
            throw new Exception("账户错误：用户名错误");
        }
        if(!tmp.password.equals(password)){
            throw new Exception("账户错误：密码错误");
        }
        
        currentUser = tmp;
    }
    
    //存款 视图
    protected void depositView(){
        System.out.println();        
        System.out.println("存款");
        
        System.out.print("存款额度: ");
        long acount = scan.nextLong();
        
        try{
            depositController(acount);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    //存款 控制器
    protected void depositController(long acount) throws Exception{
        try{
            currentUser.deposit(acount);
        }catch(Exception e){
            throw e;
        }
    }
    
    //取款 视图
    protected void withdrawView(){
        System.out.println();
        System.out.println("取款");
        
        System.out.print("取款额度: ");
        long acount = scan.nextLong();
        
        try{
            withdrawController(acount);
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
    }
    
    //取款 控制器
    protected void withdrawController(long acount) throws Exception{
        try{
            currentUser.withdraw(acount);
        }catch(Exception e){
            throw e;
        }
    }
    
    //查看余额 视图
    protected void theBlanceView(){
        long msg = currentUser.theBlance();
        System.out.println("您的余额: " + msg);
    }
    
    //判断userList中是否存在cardId
    protected int cardIdIndexOf(String cardId){
                
        int index = -1;
        for(int i = 0; i < userList.size(); i++){
            User tmp = userList.get(i);
            if(tmp.cardId.equals(cardId)){
                index = i;
                break;
            }
        }
        return index;
    }
}